package nl.Viewer.ViewManager;

import java.awt.Dimension;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import java.beans.PropertyVetoException;

import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;

import nl.Viewer.Application;

/**
 * This class is extending a JDesktopPane and is responsible for managing window frames containing the views.
 * 
 * @author Jeroen Verschuur
 * 
 */

public class ViewDesktop extends JDesktopPane implements ContainerListener {
   private static final long serialVersionUID = -1;
   public final static int   TILE_HORIZONTAL  = 1;
   public final static int   TILE_VERTICAL    = 2;

   public ViewDesktop() {
      super();
      // this listener is used to remove the parameter/orientation tabs when a frame is removed without closing it
      // for example with (removeAll)
      this.addContainerListener( this );
   }

   public void addFrame( ViewFrame frame ) {
      add( frame );
      frame.setVisible( true );
      try {
         frame.setSelected( true );
      }
      catch( java.beans.PropertyVetoException e ) {
      }
   }

   public void tileFrames() {
      // check the shape of the desktop area, width > height: horizontal tiling, otherwise vertical tiling
      if( getSize().width > getSize().height )
         tileFrames( TILE_HORIZONTAL );
      else
         tileFrames( TILE_VERTICAL );
   }

   public void tileFrames( int horizontalOrVertical ) {
      // How many frames do we have?
      JInternalFrame[] allframes = getAllFrames();
      int count = allframes.length;
      if( count == 0 )
         return;

      // Determine the necessary grid size
      int sqrt = (int)Math.sqrt( count );
      int rows = sqrt;
      int cols = sqrt;

      if( horizontalOrVertical == TILE_VERTICAL ) {
         if( rows * cols < count ) {
            rows++;
            if( rows * cols < count ) {
               cols++;
            }
         }
      } else {
         if( rows * cols < count ) {
            cols++;
            if( rows * cols < count ) {
               rows++;
            }
         }
      }

      // Define some initial values for size & location.
      Dimension size = getSize();

      int w = size.width / cols;
      int h = size.height / rows;
      int x = 0;
      int y = 0;

      // Iterate over the frames, deiconifying any iconified frames and then
      // relocating & resizing each.
      for( int i = 0; i < rows; i++ ) {
         for( int j = 0; j < cols && ( ( i * cols ) + j < count ); j++ ) {
            JInternalFrame f = allframes[( i * cols ) + j];

            if( !f.isClosed() && f.isIcon() ) {
               try {
                  f.setIcon( false );
               }
               catch( PropertyVetoException ignored ) {
               }
            }

            getDesktopManager().resizeFrame( f, x, y, w, h );
            x += w;
         }
         y += h; // start the next row
         x = 0;
      }
   }

   public void frameClosed( ViewFrame frame ) {
      tileFrames();
   }

   public void componentAdded( ContainerEvent e ) {
   }

   public void componentRemoved( ContainerEvent e ) {
      // when we call removeAll
      Application.viewManager.setOrientationAndParameterTabs( null );
      tileFrames();
   }
}
